package com.twu.biblioteca.domain;

public class Book implements LibraryEntity, Withdrawable {
    private final String bookName;
    private final String bookAuthor;
    private final int dateOfPublication;
    private final String id;
    private LibraryUser withdrawnBy;
    private boolean isWithdrawn;



    public Book(String id, String bookName, String bookAuthor, int dateOfPublication) {
        this.id = id;
        this.bookName = bookName;
        this.bookAuthor = bookAuthor;
        this.dateOfPublication = dateOfPublication;
    }

    @Override
    public boolean equals(Object other) {
        //check if correct instance before casting
        Book otherBook = (Book) other;
        return this.bookName == otherBook.bookName &&
                this.bookAuthor == otherBook.bookAuthor &&
                this.dateOfPublication == otherBook.dateOfPublication;
    }

    @Override
    public String toString() {
        return String.format("%s - %s. %s. %d.%s", id, bookName, bookAuthor, dateOfPublication, showLoaneeIfExists());
    }
    
    private String showLoaneeIfExists(){
        return (this.isWithdrawn) ?
                String.format(" Withdrawn By: %s.", withdrawnBy.getId()) :
                "";
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public int compareTo(Object o) {
        return this.getId().compareTo(((Book) o).getId());
    }

    @Override
    public void setWithdrawn(LibraryUser user) {
        isWithdrawn = true;
        withdrawnBy = user;
    }

    @Override
    public void setReturned() {
        this.isWithdrawn = false;
        this.withdrawnBy = null;
    }
}
